<?php
namespace Home\Controller;
use Common\Library\BusinessException;
use Common\Library\Constant;
use Think\Controller;
use Think\Log;

class WebappController extends Controller {

    public function activity() {
        $this->assign("draw_one", 'http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/firstBonus');
        $this->assign("draw_two", 'http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend');
        $this->display();
    }

    public function firstBonus()
    {
        $this->assign("saveBonus", 'http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/saveBonus');
        $this->display('firstBonus');
    }

    public function recommend()
    {
        $this->assign("saveRecommend", 'http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/saveRecommend');
        $this->display();
    }

    public function saveBonus()
    {
        $name = I("post.name", '', 'string');
        $mobile = I("post.mobile", '', 'string');
        if(!$name || !$mobile || strlen($mobile) < 11){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/firstBonus', 1001);
            return false;
        }
        //校验该用户是不是新用户
        $customer = M('Customer')->where(['mobile'=>$mobile])->count('order_count');
        $act = M('CustomerActivity')->where(['recommend_mobile'=>$mobile])->count('id');
        if($customer || $act){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/firstBonus', 1002);
            return false;
        }
        //记录活动数据
        $data = [
            'recommend_name'   => $name,
            'recommend_mobile' => $mobile,
            'type'   => 1
        ];

        if(false === M('CustomerActivity')->add($data)){
            echo "<script>alert('申请失败')</script>";
            return false;
        }else{
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/firstBonus', 1000);
            return false;
        }
    }

    public function saveRecommend()
    {
        $name = I("post.name", '', 'string');
        $mobile = I("post.mobile", '', 'string');
        $recommend_name = I("post.recommend_name", '', 'string');
        $recommend_mobile = I("post.recommend_mobile", '', 'string');
        if(!$name || strlen($mobile) < 11){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend', 1001);
            return false;
        }
        if(!$recommend_name || strlen($recommend_mobile) < 11){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend', 1003);
            return false;
        }
        if($recommend_mobile == $mobile){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend', 1004);
            return false;
        }
        //校验该用户是不是新用户
        $customer = M('Customer')->where(['mobile'=>$recommend_mobile])->sum('order_count');
        $act = M('CustomerActivity')->where(['recommend_mobile'=>$recommend_mobile])->count('id');
        if($customer || $act){
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend', 1002);
            return false;
        }
        //记录活动数据
        $data = [
            'name'   => $name,
            'mobile' => $mobile,
            'recommend_name'   => $recommend_name,
            'recommend_mobile' => $recommend_mobile,
            'type'   => 2
        ];

        if(false === M('CustomerActivity')->add($data)){
            echo "<script>alert('申请失败')</script>";
            return false;
        }else{
            $this->responseURL('http://'.$_SERVER['HTTP_HOST'].'/Home/Webapp/recommend', 1000);
            return false;
        }
    }

    function responseURL($url = null, $code = null)
    {
        if($url != null){
            if($code){
                $url .= '?code='.$code;
            }
            echo "<script>window.location='{$url}'</script>";
        }
    }
}
